<?php

class Setup_AvatarController extends Ext_Controller_Action_Front
{
    public function init()
    {
        parent::init();
        
        Zend_Registry::set('_use_adapter', 'master');
    }
    
    public function preDispatch()
    {
        parent::preDispatch();
        
        $userId = Ext_Auth_Front::getInstance()->getIdentity();
        if(!$userId){
            return "";
        }
 
        $roleId = Ext_User::getInstance()->show($userId)->buildRole()->role->roleId;
        $acl = Ext_Acl::getInstance()->build($roleId);
        if(!$acl->isAllowed($roleId, 'setup', 'avatar')){
            $this->_forward("show-access-denied", "auth", "account");
        }
    }
    
    public function showPanelAction()
    {
        $userId = Ext_Auth_Front::getInstance()->getIdentity();

        $avatarRow = App_Model_DbTable_User_Avatar::getInstance()->getRowByUserId($userId);
        if($avatarRow){
            $imageId = $avatarRow['imageId'];
            $photo  = new Ext_Image_Show();
            $photo->build($imageId);
            
            $this->view->imageId  = $imageId;
            $this->view->photo  = $photo;
            $this->view->avatar = new Ext_Avatar_Show($userId);
        }
        
        $config = Ext_Config::getInstance()->getConfig('image');
        $this->view->big = $config->avatar->size->big;
    }
    
    public function savePostAction()
    {
        $viewResult = new Ext_View_Result();
        
        $userId = Ext_Auth_Front::getInstance()->getIdentity();
        
        $userAvatar = App_Model_User_Avatar::getInstance()->getRowByUserId($userId);
        $imageId = $userAvatar['imageId'];
        
        $saver = new Ext_Avatar_Save($imageId);
        $resulter = $saver->save($this->_request->getPost());
        if(!$resulter->isValid()){
            $viewResult->setMessages($resulter->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        $avatarId = $resulter->getData();
        
        $row = array(
            'imageId'  => $imageId,
            'avatarId' => $avatarId,
        );
        App_Model_DbTable_User_Avatar::getInstance()->addOld($row, $userId);
        
        $avatar = new Ext_Avatar_Show($userId);
        $data = array(
            'big'    => $avatar->big,
            'middle' => $avatar->middle,
            'small'  => $avatar->small,
            'thumb'  => $avatar->thumb,
        );
        $viewResult->setData($data);
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }
    
    public function saveUploadAction()
    {
        $viewResult = new Ext_View_Result();
        $viewResult->setLayout("layout_result-xml");
        
        $uploador = new Ext_Image_Upload();
        $uploadResultor = $uploador->upload();
        if(!$uploadResultor->isValid()){
            $viewResult->addMessages($uploadResultor->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        $uploadFile = $uploadResultor->getData()->file;

        $savor = new Ext_Image_Save();
        $saveResultor = $savor->save($uploadFile);
        if(!$saveResultor->isValid()){
            $viewResult->addMessages($saveResultor->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        $imageId = $saveResultor->getData();

        $userId = Ext_Auth_Front::getInstance()->getIdentity();
        $userAvatar = App_Model_User_Avatar::getInstance()->getRowByUserId($userId);
        if($userAvatar){
            $row = array(
                'imageId'  => $imageId,
            );
            App_Model_DbTable_User_Avatar::getInstance()->addOld($row, $userId);
        }
        
        $photo  = new Ext_Image_Show();
        $photo->build($imageId);
        //Zend_Debug::dump($photo, '$photo');
        
        $viewResult->setData(array('url'=>$photo->source, 'imageId'=>$imageId));
        $viewResult->addMessage($this->translater->_("Submit success."));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
        
    }
}

?>